/*
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: Feb 22, 2011
 * Time: 12:38:20 PM
 */
package mycomp.chlor

import java.lang.String
import com.google.gdata.client.docs.DocsService
import java.net.URL
import com.google.gdata.data.docs.{DocumentListEntry, DocumentListFeed}
import scaladuct.{First, Ref, Starter}
import java.util.{Date, Iterator}

class AwesomeStarter extends Starter[String] {
  def newInstance() = phaseGetInput(null, false, new Date(), NiceEnum1.NiceValue1)

  def phaseGetInput(@Ref gdocsStarter: Starter[DocsService], flag: Boolean, when: Date, nice: NiceEnum1) = {

    val gdocs = gdocsStarter.newInstance
    try {
      val feedUrl = new URL("https://docs.google.com/feeds/default/private/full")
      val resultFeed: DocumentListFeed = gdocs.getFeed(feedUrl, classOf[DocumentListFeed])
      val builder = new StringBuilder

      val iterator: Iterator[DocumentListEntry] = resultFeed.getEntries().iterator
      while (iterator.hasNext) {
        val entry: DocumentListEntry = iterator.next
        builder.append(entry.getTitle().getPlainText()).append(", ")
      }

      phaseGetInput2(builder.toString, "")
    }
    finally {
      gdocsStarter.destroy(gdocs)
    }
  }

  def phaseGetInput2(str: String, @First decoration: String) = {
     decoration + str + decoration
  }

  def destroy(instance: String) = null
}